<?php

//后台用户管理
namespace App\Http\Controllers\Admin;

use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Mail;
class UserController extends BaseController
{
    //用户列表
    public  function index(Request $request){
        if(request()->wantsJson()){
            $page  = $request->input('page',1);
            $limit = min(100,$request->input('limit',10)) ;
            $where =[];
            $username = $request->input('username');
            if($username){
                $where =[['username', 'like', "%{$request->input('username')}%"]];
            }
            $total = User::count();
            $data = User::where($where)->orderBy('id', 'DESC')
                    ->offset(($page - 1) * $limit)
                    ->limit($limit)->get();
            return response()->json([
                'message' => '成功',
                'count' => $total,
                'code'=>0,
                'data' => $data
            ]);
        }else{
            return view('admin.user.index');
        }
    }
    //添加显示
    public function create(){
        return view('admin.user.create');
    }
    //添加处理
    public function store(Request $request){
        if($request->wantsJson()){
            $this->validate($request,[
                //用户唯一行验证
                'username'=>'required|unique:users,username',
                //手机号验证
                'phone'=>'required',
                //密码验证
                'password'=>'required|confirmed',
            ]);
            //排除 元素
            $data = $request->except(['_token','password_confirmation']);//排除 元素
            //添加用户入库
            $data['password'] = bcrypt($data['password']);
            $userData  = User::create($data);
            if($userData){
                //发邮件 注册成功！
//                $userData['password'] =$request->get('password');
//                Mail::send('mail.useradd',compact('userData'),function (Message $message) use ($userData){
//                    $message->to($userData->email);
//                    $message->subject("添加用户成功！！");
//                });
                return  $this->reSuJson('添加成功');
            }
            return $this->reErJson('添加失败');
        }
    }

    //删除
    public  function  del(int $id){
        //软删除
        $re = User::find($id)->delete();
        ///强制删除 配置软删除后 实际删除
//        $re = User::find($id)->forceDelete();
        if($re) return  $this->reSuJson('删除成功');
        return $this->reErJson('删除失败');
    }

    //还原删除
    public function  restore(int $id){
        //恢复删除数据
        $re = User::onlyTrashed()
            ->where('id', $id)
            ->delete();

        if($re) return  $this->reSuJson('恢复成功');
        return $this->reErJson('恢复失败');
    }

    //批量删除
    public function  delall(Request $request){
        if(User::destroy($request->get('ids')))
        {
            return $this->reSuJson('恢复成功');
        }
        return $this->reErJson('恢复失败');
    }

    //角色分配
    public function  role(Request $request,int $id){
        //当前用户角色
        $user = User::find($id);
        //判断是否是 修改操作
        if($request->isMethod('post')){
            $post = $this->validate($request,[
                'role_id'=>"required"
            ]);
            //修改 角色id
            $re = $user->update($post);
            if($re){
                return  $this->reSuJson('成功');
            }
            return $this->reErJson('失败');
        }
        //获取所有的校色
        $roleAll = Role::all();
        return  view('admin.user.role',compact('user','roleAll'));
    }

    //修改显示
    public function  edit(int $id){
        return  view('admin.user.edit',['user'=>User::find($id)]);
    }
    //修改显示
    public function  update(Request $request,int $id){
        $this->validate($request,[
            //手机号验证
            'phone'=>'required',
            //密码验证
            'password'=>'nullable|confirmed',
        ]);
        $user = User::find($id);
        if($user){
            $post = $request->only(['truename','phone','email','sex']);
            if($request['password']) $post['password'] =bcrypt($request['password']);
            if($user->update($post)){
                return  $this->reSuJson('修改成功');
            }
        }
        return $this->reErJson('修改失败');
    }
}
